'use strict';

const conn = require('../../libs/Conn');
const util = require('../../libs/Utils');
const moment = require('moment');

module.exports = async function (params) {
    let insertObj = {
        engineering_area:params.engineering_area||null,
        engineering_address:params.engineering_address,
        aboveGround_floor:params.aboveGround_floor||null,
        underGround_floor:params.underGround_floor||null,
        admission_construction_date:params.admission_construction_date||null,
        admission_construction_status:params.admission_construction_status||null,
        supervisory_registration_certificate:params.supervisory_registration_certificate,
        planning_certificate:params.planning_certificate,
        drawing_number:params.drawing_number,
        construction_permit:params.construction_permit,
        construction_unit_employer_id:params.construction_unit_employer_id,
        design_unit_id:params.design_unit_id,
        construction_unit_contractor_id:params.construction_unit_contractor_id,
        supervisory_unit_id:params.supervisory_unit_id,
        survey_unit_id:params.survey_unit_id,
        drawing_unit_id:params.drawing_unit_id,
        construction_unit_employer_person:params.construction_unit_employer_person,
        design_unit_person:params.design_unit_person,
        construction_unit_contractor_person:params.construction_unit_contractor_person,
        supervisory_unit_person:params.supervisory_unit_person,                         
        survey_unit_person:params.survey_unit_person,
        drawing_unit_person:params.drawing_unit_person,
    };
    let engineeringObj = {
        startDate:params.startDate,
        principal_id:params.principal_id,
        building_id:params.building_use,
        structure_id:params.structure_id,
        engineering_name:params.engineering_name
    };
    let insertUserSql = ``;
    if(params.user_ids){
        let user_ids=params.user_ids;
        user_ids=user_ids.split(',');
        let deleteSql = `DELETE FROM engineering_user_middle WHERE engineering_id = ?`;
        await conn(deleteSql,[params.engineering_id]);
        for (let user_id in user_ids){
            insertUserSql += `INSERT INTO engineering_user_middle (engineering_id, user_id) VALUES ('${params.engineering_id}', '${user_ids[user_id]}');`;
        }
        await conn(insertUserSql);
    }
    let sql =  `UPDATE engineering_information_file  SET ? WHERE engineering_id = ?;`;
    let engineeringSql = `UPDATE engineering_file  SET ? WHERE engineering_id = ? ;`;

    await conn(engineeringSql, [engineeringObj,params.engineering_id]);
    await conn(sql, [insertObj,params.engineering_id]);
    return {success: true};
};